const mysql = require('../db/mysql57');

const washDao = {
    getAllWashRecord(reqId, uid) {
        let sql = `select
                dw.id as id,
                d.devID as devID,
                p.name as province,
                c.name as city,
                dt.type as devType,
                dp.hospital as hospital,
                dp.name as department,
                DATE_FORMAT(dw.startTime, '%Y-%m-%d %H:%i:%s') as time,
                dw.record as record,
                dw.result as result
            from DeviceWash as dw
            left join Device as d on dw.deviceId = d.id
            left join Department as dp on d.departmentId = dp.id
            left join City as c on dp.cityId = c.id
            left join Province as p on c.provinceId = p.id
            left join DeviceType as dt on d.deviceTypeId = dt.id
            where dw.is_del = ?
            order by dw.id desc`;
        let data = [ 'false' ];
        return mysql.sqlExec(sql, data);
    },
    getAllWashRecord_usePower(reqId, uid) {
        let sql = `select
                dw.id as id,
                d.devID as devID,
                p.name as province,
                c.name as city,
                dt.type as devType,
                dp.hospital as hospital,
                dp.name as department,
                DATE_FORMAT(dw.startTime, '%Y-%m-%d %H:%i:%s') as time,
                dw.record as record,
                dw.result as result
            from DeviceWash as dw
            left join Device as d on dw.deviceId = d.id
            left join Department as dp on d.departmentId = dp.id
            left join City as c on dp.cityId = c.id
            left join Province as p on c.provinceId = p.id
            left join DeviceType as dt on d.deviceTypeId = dt.id
            where dw.is_del = ? AND dp.adminId = ?
            order by dw.id desc`;
        let data = [ 'false', uid ];
        return mysql.sqlExec(sql, data);
    },
    getWashRecordInfo(reqId, uid, washRecordId) {
        let sql = `select
                dw.id as id,
                d.devID as devID,
                dt.type as deviceType,
                dt.name as deviceName,
                p.name as province,
                c.name as city,
                dp.hospital as hospital,
                dp.name as department,
                DATE_FORMAT(dw.startTime, '%Y-%m-%d %H:%i:%s') as startTime,
                DATE_FORMAT(dw.endTime, '%Y-%m-%d %H:%i:%s') as endTime,
                dw.result as result,
                dw.record as record,
                dw.doctorName as doctorName,
                dw.patientName as patientName,
                dw.patientSex as patientSex,
                dw.patientID as patientID,
                dw.diagnosisNumber as diagnosisNumber,
                dw.hospitalizationNumber as hospitalizationNumber,
                dw.remark as remark
            from (
                select * from DeviceWash where id = ? and is_del = ?
            ) as dw
            left join Device as d on dw.deviceId = d.id
            left join Department as dp on d.departmentId = dp.id
            left join DeviceType as dt on d.deviceTypeId = dt.id
            left join City as c on dp.cityId = c.id
            left join Province as p on c.provinceId = p.id`;
        let data = [ washRecordId, 'false' ];
        return mysql.sqlExec(sql, data);
    },
    getWashRecordInfo_usePower(reqId, uid, washRecordId) {
        let sql = `select
                dw.id as id,
                d.devID as devID,
                dt.type as deviceType,
                dt.name as deviceName,
                p.name as province,
                c.name as city,
                dp.hospital as hospital,
                dp.name as department,
                DATE_FORMAT(dw.startTime, '%Y-%m-%d %H:%i:%s') as startTime,
                DATE_FORMAT(dw.endTime, '%Y-%m-%d %H:%i:%s') as endTime,
                dw.result as result,
                dw.record as record,
                dw.doctorName as doctorName,
                dw.patientName as patientName,
                dw.patientSex as patientSex,
                dw.patientID as patientID,
                dw.diagnosisNumber as diagnosisNumber,
                dw.hospitalizationNumber as hospitalizationNumber,
                dw.remark as remark
            from (
                select * from DeviceWash where id = ? and is_del = ?
            ) as dw
            left join Device as d on dw.deviceId = d.id
            left join Department as dp on d.departmentId = dp.id
            left join DeviceType as dt on d.deviceTypeId = dt.id
            left join City as c on dp.cityId = c.id
            left join Province as p on c.provinceId = p.id
            where dp.adminId = ?`;
        let data = [ washRecordId, 'false', uid ];
        return mysql.sqlExec(sql, data);
    },
    updateWashRecord(reqId, uid, id, doctorName, patientName, patientSex, patientID, diagnosisNumber, hospitalizationNumber, remark) {
        let sql = `update DeviceWash
            set doctorName = ?,
            patientName = ?,
            patientSex = ?,
            patientID = ?,
            diagnosisNumber = ?,
            hospitalizationNumber = ?,
            remark = ?
            where id = ? and is_del = ?`;
        let data = [ doctorName, patientName, patientSex, patientID, diagnosisNumber, hospitalizationNumber, remark, id, 'false' ];
        return mysql.sqlExec(sql, data);
    },
    updateWashRecord_usePower(reqId, uid, id, doctorName, patientName, patientSex, patientID, diagnosisNumber, hospitalizationNumber, remark) {
        let sql = `update DeviceWash
            left join Device on Device.id = DeviceWash.deviceId
            left join Department on Device.departmentId = Department.id
            set DeviceWash.doctorName = ?,
            DeviceWash.patientName = ?,
            DeviceWash.patientSex = ?,
            DeviceWash.patientID = ?,
            DeviceWash.diagnosisNumber = ?,
            DeviceWash.hospitalizationNumber = ?,
            DeviceWash.remark = ?
            where DeviceWash.id = ? and DeviceWash.is_del = ? and Department.adminId = ?`;
        let data = [ doctorName, patientName, patientSex, patientID, diagnosisNumber, hospitalizationNumber, remark, id, 'false', uid ];
        return mysql.sqlExec(sql, data);
    },
    delWashRecordInfo(reqId, uid, washRecordId) {
        let sql = `update DeviceWash set is_del = ? where id = ?`;
        let data = [ 'true', washRecordId ];
        return mysql.sqlExec(sql, data);
    }
};

module.exports = washDao;